package com.fantabest.common.server.dao;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.apache.log4j.Logger;

import com.fantabest.common.PMF;
import com.fantabest.common.entity.Manager;
import com.fantabest.common.entity.Season;
import com.fantabest.common.entity.Team;

public class TeamDao {

	private static final Logger log = Logger.getLogger(SeasonDao.class);

	private static TeamDao instance;

	public static TeamDao getInstance() {
		if (instance == null)
			instance = new TeamDao();
		return instance;
	}
	
	public void save(Team team)	{
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(team);
		} catch (Exception e) {
			e.printStackTrace();
		}	finally	{
			pm.close();
		}
	}
	
	public Team findByManager(Manager manager,Season season)	{
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(Team.class);
		q.setFilter("manager == pmanager");
		q.setFilter("season == pseason");
		q.declareParameters("Manager pmanager, Season pseason");
		Team result = null;
		try {
			Team sqr = (Team) q.execute(manager,season);
			result = (Team)pm.detachCopy(sqr);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			q.closeAll();
		}
		return result;

	}
}
